﻿CREATE PROCEDURE [dbo].[Referal_AddReferal]
	@userId UNIQUEIDENTIFIER,
	@textId BIGINT,
	@refUserId UNIQUEIDENTIFIER 
AS
BEGIN
	IF @refUserId = @userId
		RETURN

	IF EXISTS (SELECT * FROM [user_Referals] WHERE [UserId] = @userId AND [TextId] = @textId)
	BEGIN
		UPDATE [dbo].[user_Referals]
		SET [ReferalUserId] = @refUserId
		WHERE [UserId] = @userId AND [TextId] = @textId
	END
	ELSE
	BEGIN
		INSERT INTO [dbo].[user_Referals] ([UserId], [ReferalUserId], [TextId])
		VALUES (@userId, @refUserId, @textId)
	END
END